Skip to content

Name the guarded API in ThrowIfVerifyHasBeenRun - #1864

Merged
SimonCropp merged 2 commits into
mainfrom
fix-throwif-api-frame
Aug 17, 2026
Merged

Name the guarded API in ThrowIfVerifyHasBeenRun#1864
SimonCropp merged 2 commits into
mainfrom
fix-throwif-api-frame

Conversation

@SimonCropp

@SimonCropp SimonCropp commented Aug 17, 2026

Copy link
Copy Markdown
Member

The message named the calling code as "The API":

The API 'MyTests.SomeTest' must be called prior to any Verify has run.

which points at the wrong place to move into a [ModuleInitializer].

First attempt read frame 0 instead of frame 1, and CI caught that it does not hold. Whether the guarded API has a stack frame of its own is a JIT decision: on net48 in release VerifierSettings.IgnoreMembers is inlined into its caller, so frame 0 is the caller. No frame index is correct — with the original GetFrame(1) the same net48 release run names Xunit.Assert.RecordException, two frames adrift, because the test lambda is inlined too.

So the name now comes from [CallerMemberName], which the compiler fills in at each of the 111 call sites. It cannot drift with inlining, costs nothing at run time, and needs no StackTrace (which also makes it safe under trimming, where frames can be stripped).

Two consequences worth calling out:

  • The declaring type is gone from the messageCallerMemberName only supplies the member. It now reads The API 'IgnoreMembers' must be called prior…. No snapshot or doc captures this message, so nothing else moved.
  • ThrowIfVerifyHasBeenRun gains an optional parameter. Source compatible, but a binary break for anything already compiled against the no-arg signature. Every caller in this repo is rebuilt together; a third party adapter shipped as a prebuilt assembly would need a recompile. That seems acceptable for 32.0.0, but it is your call.

ThrowIfVerifyHasBeenRunTests.NamesTheApi calls a real guarded API and asserts the message names it and not the test. Verified across Debug and Release × net48 and net11.0 this time, since that axis is exactly what the first attempt missed.

Release suites: Verify.Tests net48 (1197), net11.0 (1287), net9.0 (1293); StaticSettingsTests, Verify.NUnit.Tests, Verify.MSTest.Tests, Verify.XunitV3.Tests, ApplyScrubbersTests, Verify.ExceptionParsing.Tests all green.

@SimonCropp SimonCropp added this to the 32.0.0 milestone Aug 17, 2026
The message named the calling code as "The API", pointing at the wrong place to
move into a module initializer. Reading the name off a StackTrace cannot fix
that: whether the guarded API has a frame of its own depends on the JIT, and on
net48 in release it is inlined into the caller, so no frame index is right.

The name now comes from CallerMemberName, which the compiler fills in at each
call site, so it is the same under every runtime and configuration. That drops
the declaring type from the message, since only the member name is available.
@SimonCropp
SimonCropp force-pushed the fix-throwif-api-frame branch from acc7c7b to 2c029cf Compare August 17, 2026 12:51
@SimonCropp
SimonCropp merged commit 1e73f28 into main Aug 17, 2026
5 checks passed
@SimonCropp
SimonCropp deleted the fix-throwif-api-frame branch August 17, 2026 22:25
This was referenced Aug 26, 2026
This was referenced Aug 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant